**** Constructing the dataset for analysis of updated data *****
** Making the dataset on updatet ambitions 
import excel "$path/Original Excel_Files/NDC gathering_YRdP.xlsx", sheet("Targets") firstrow

//renaming variables that will be used in the analysis
rename Countrylist countryname
rename countrycodes iso3
rename pledgedwarming Dupont_2

keep iso3 Dupont_2

//Dropping observations with missing iso3 and Ambition values
drop if iso3==""


//Saving the dataset for later merge
cd "$path/New datasets"
save Dupont_2.dta, replace
clear all


** Making the ambiguity dataset for updated NDCs. 
import delimited "$path/New datasets/new-updated-ndc-data.csv"

replace country_name = subinstr(country_name, `", "', "",.)

export delimited using "$path/New datasets/new-updated-ndc-data-cleaned.csv", replace

clear all

//Import with the correct values
import delimited "$path/New datasets/new-updated-ndc-data-cleaned.csv", bindquote(nobind) 

//Observation of PERU are moved one step to the right. Ensuring that PERU has correct values for costs of ccm, reducing non co2 gases and the 
//conditionality variables. 

replace costs_of_ccm_class=costs_of_ccm_details if country_code=="PER"
replace reducing_non_co2_gases_class=reducing_non_co2_gases_details if country_code=="PER"
replace conditionality_of_mitigation_fin=v88 if country_code=="PER"
replace conditionality_of_capacity_build=v94 if country_code=="PER"
replace conditionality_of_technology_tra=v92 if country_code=="PER"



// Dropping observation with missing values on any of the ambiguity dimension
foreach var in a b c d e f {
	gen `var'=.
}



forval i=0/6 {
	replace a=1 if type_of_target_class==`i'
	replace b=1 if costs_of_ccm_class=="`i'"
	replace c=1 if reducing_non_co2_gases_class=="`i'"
	replace d=1 if conditionality_of_mitigation_fin=="`i'"
	replace e=1 if conditionality_of_technology_tra=="`i'"
	replace f=1 if conditionality_of_capacity_build=="`i'"
}

drop if a==. | b==. | c==. | d==. | e==. | f==. 

destring type_of_target_class costs_of_ccm_class reducing_non_co2_gases_class conditionality_of_mitigation_fin conditionality_of_technology_tra conditionality_of_capacity_build, replace


//Renaming the common country identifier
rename country_code iso3

//Dropping if the iso3 code is less than two or more than three characters, or containing numeric values
drop if length(iso3) < 2 | length(iso3)>3
drop if real(iso3)!=.

//inserting the EU values for all possible EU members in the dataset
//Checking the distribution of duplicates prior to inserting the
//EU values which can be compared to the distribution afterwards.
gen x=1 
sort type_of_target_class-g20_details
quietly by type_of_target_class-g20_details:  gen dup = cond(_N==1,0,_n) 
tab dup

replace x=. if iso3=="EU"

//Creating the new EU observations
foreach i in AUT BEL BGR CYP CZE DEU DNK ESP EST FIN FRA GRC HRV HUN IRL ITA LTU LUX LVA MLT NLD /// 
POL PRT ROU SVK SVN SWE {
	set obs `=_N+1'
	replace iso3="`i'" if iso3=="" 
}

replace iso3="A" if iso3=="EU"
sort iso3

//Inserting the EU variables
qui ds
local all `r(varlist)'
local names country_name iso3
local ambiguity: list all - names 

foreach var in `ambiguity' {
foreach i in AUT BEL BGR CYP CZE DEU DNK ESP EST FIN FRA GRC HRV HUN IRL ITA LTU LUX LVA MLT NLD /// 
POL PRT ROU SVK SVN SWE {
	replace `var'=`var'[1] if iso3=="`i'"
}
}

//See if it worked
preserve
replace x=1 if iso3=="A"
drop if x!=1
sort type_of_target_class-g20_details 
quietly by type_of_target_class-g20_details:  gen dup1 = cond(_N==1,0,_n) 
tab dup1
restore



//Drop the European Union itself
drop if iso3=="A"

// Saving the dataset. To be merged later.
cd "$path/New datasets"

save uncertainty_updated.dta, replace
clear all


***** Control variables: merging with uncertainty_updated and Dupont_2 *******
import excel "$path/Original Excel_Files/ambition-controls-apr24.xlsx", sheet("sheet1") firstrow

cd "$path/New datasets"

//Merge with uncertainty_updated
merge 1:1 iso3 using uncertainty_updated

keep if _merge==3
drop _merge


//Merging with the second round Dupont variables.
merge 1:1 iso3 using Dupont_2.dta
drop if _merge==2 
drop _merge

*** 
cd "$path"
run 6_NDC_analysis_2.do





